import { setBank } from '../../../api/index.js'

// pages/editWithdrawl/bankItem/bankItem.js
Component({

  /**
   * 页面的初始数据
   */
  data: {
    accountList : [
      '个人',
      '企业'
    ],
    selectIndex: 0,
    type: 'personal',
    accountName: '',
    accountNumber: ''
  },
  methods: {
    change: function (e) {
      const { detail: { value } } = e
      this.setData({
        selectIndex: value,
        type: value === '0' ? 'person' : 'company'
      })
    },

    changeInfo: function(e) {
      const { type, accountName, accountNumber }  =this.data
      const pages = getCurrentPages()
      const prevPage = pages[pages.length - 2]
      const {currentTarget:{dataset:{info}}, detail:{value}} = e
      console.log(value, 'render change value')

      if(info === 'bankName') {
        this.setData({
          accountName: value
        })
      }

      if(info === 'bankNumber') {
        this.setData({
          accountNumber: value
        })
      }
    },
    valid: function () {
      const { type, accountName, accountNumber } = this.data

      if (!accountName) {
        return wx.showToast({
          title: '请输入账号名称',
          icon: 'none'
        })
      }

      if (!accountNumber) {
        return wx.showToast({
          title: '请输入银行账号',
          icon: 'none'
        })
      }

      return true
    },
    onSubmit: function () {
      const flag = this.valid()
      const { type, accountName, accountNumber } = this.data
      console.log(this.data, 999)


      if (flag) {
        setBank({ card_type: type, bank_name: accountName, card_number: accountNumber })
          .then(res => {
            return wx.showToast({
              title: '保存成功',
              icon: 'none'
            })
          })
          .catch(e => {
            console.log(e, '设置银行卡失败')
          })
      }
    }
  }  
})